import request from '@/utils/request'

// 分页查询质检单信息
export const queryQualityInspectionService = (params: any) => {
  return request.get('/quality-manager/queryQualityInspection', { params })
}

// 新增异常追踪
export const addExceptionTrackingService = (data: any) => {
  return request.post('/quality-manager/addExceptionTracking', data)
}

// 审核质检单
export const reviewQualityInspectionService = (reviewInspectionDTO: any) => {
  return request.put('/quality-manager/reviewQualityInspection', reviewInspectionDTO)
}


// 获取异常列表
export const getExceptionListService = (params: any) => {
  return request.get('/quality-manager/exception-list', { params })
}

// 更新异常追踪
export const updateExceptionTrackingService = (data: any) => {
  return request.put('/quality-manager/updateExceptionTracking', data)
}

// 获取异常详情
export const getExceptionDetailService = (exceptionNumber: string) => {
  return request.get(`/quality-manager/exception-detail/${exceptionNumber}`)
}

// 获取异常追踪记录
export const getExceptionTrackingService = (exceptionNumber: string) => {
  return request.get(`/quality-manager/exception-tracking/${exceptionNumber}`)
}

//导出质检单
export const exportInspectionService = (params: any) => {
  return request.get('/quality-manager/export/inspection', { 
    params,
    responseType: 'blob',
    paramsSerializer: {
      serialize: (params: any) => {
        const searchParams = new URLSearchParams()
        if (params.inspectionIds && Array.isArray(params.inspectionIds)) {
          params.inspectionIds.forEach((id: number) => {
            searchParams.append('inspectionIds', id.toString())
          })
        }
        return searchParams.toString()
      }
    }
  })
}

// 获取不合格质检列表
export const listUnqualifiedInspectionService = () => {
  return request.get('/quality-manager/list/inspection')
}

// 获取异常跟踪记录时间线
export const getExceptionTrackingTimelineService = (exceptionNumber: string) => {
  return request.get(`/quality-manager//tracking-timeline/${exceptionNumber}`)
}

